package com.loto.insurance.mis.web.utils;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.loto.insurance.framework.EhcacheUtil;
import com.loto.insurance.mis.web.entity.SessionUser;
import com.loto.insurance.mis.web.entity.config.ChildMenu;
import com.loto.insurance.mis.web.entity.config.Menu;
import com.loto.insurance.mis.web.entity.config.MenuConfig;
import com.loto.utils.XML;

public class Config {
  public static MenuConfig getMenuConfigFile(HttpServletRequest request) {
	HttpSession session = request.getSession();
	SessionUser user = (SessionUser) session.getAttribute("ins_user");
	if (user != null) {
	  String configFile = "WEB-INF/resources/config/menu/" + user.getUsername()
		  + "_menu_config.xml";
	  try {
		EhcacheUtil ehcache = EhcacheUtil.getInstance();
		Object o = ehcache.get("configFileCache", configFile);

		if (o != null) {
		  return (MenuConfig) o;
		}

		String fileName = request.getSession().getServletContext()
		    .getRealPath("/")
		    + configFile;
		MenuConfig config = XML.fromXMLFile(fileName, MenuConfig.class);
		ehcache.put("configFileCache", fileName, config);

		/*
	     * MenuConfig tmp = new MenuConfig(); List<Menu> menuList = new
	     * ArrayList<Menu>(); Menu m = new Menu(); m.setId(1); m.setName("aa");
	     * m.setIcon("aaa"); List<ChildMenu> childMenuList = new
	     * ArrayList<ChildMenu>(); ChildMenu cm = new ChildMenu(); cm.setId(11);
	     * cm.setName("bb"); cm.setParentId(1); cm.setUrl("bbb");
	     * childMenuList.add(cm);
	     * 
	     * cm = new ChildMenu(); cm.setId(12); cm.setName("cc");
	     * cm.setParentId(1); cm.setUrl("ccc"); childMenuList.add(cm);
	     * 
	     * m.setChildMenuList(childMenuList); menuList.add(m);
	     * tmp.setMenuList(menuList);
	     * 
	     * String aa = XML.toXML(tmp, MenuConfig.class);
	     */

		return config;
	  } catch (Exception e) {
		e.printStackTrace();
	  }
	}
	return null;
  }

  public MenuConfig getMenuConfigFile(String userName) {
	String configFile = "WEB-INF/resources/config/menu/" + userName
	    + "_menu_config.xml";
	try {
	  String path = this.getClass().getResource("").getPath()
		  .replaceAll("%20", " ");
	  String fileName = path.substring(0, path.indexOf("WEB-INF")) + configFile;
	  EhcacheUtil ehcache = EhcacheUtil.getInstance();
	  Object o = ehcache.get("configFileCache", configFile);
	  if (o != null) {
		return (MenuConfig) o;
	  }
	  MenuConfig config = XML.fromXMLFile(fileName, MenuConfig.class);
	  ehcache.put("configFileCache", configFile, config);
	  return config;
	} catch (Exception e) {
	  e.printStackTrace();
	}
	return null;
  }

  public MenuConfig getBaseMenuConfigFile() {
	String configFile = "WEB-INF/resources/config/menu/menu_config.xml";
	try {
	  String path = this.getClass().getResource("").getPath()
		  .replaceAll("%20", " ");
	  String fileName = path.substring(0, path.indexOf("WEB-INF")) + configFile;
	  /*EhcacheUtil ehcache = EhcacheUtil.getInstance();
	  Object o = ehcache.get("configFileCache", configFile);
	  if (o != null) {
		return (MenuConfig) o;
	  }*/
	  MenuConfig config = XML.fromXMLFile(fileName, MenuConfig.class);
	  //ehcache.put("configFileCache", configFile, config);
	  return config;
	} catch (Exception e) {
	  e.printStackTrace();
	}
	return null;
  }

  public MenuConfig getCheckedBaseMenuConfigFile(String userName) {
	MenuConfig baseConfig = getBaseMenuConfigFile();
	MenuConfig userConfig = getMenuConfigFile(userName);
	if (baseConfig != null && userConfig != null) {
	  List<Menu> userMenuList = userConfig.getMenuList();
	  for (int i = 0; i < baseConfig.getMenuList().size(); i++) {
		for (int j = 0; j < baseConfig.getMenuList().get(i).getChildMenuList()
		    .size(); j++) {
		  for (Menu userMenu : userMenuList) {
			List<ChildMenu> userChildMenu = userMenu.getChildMenuList();
			for (ChildMenu childMenu : userChildMenu) {
			  if (childMenu.getId() == baseConfig.getMenuList().get(i)
				  .getChildMenuList().get(j).getId()) {
				baseConfig.getMenuList().get(i).getChildMenuList().get(j)
				    .setIsChecked(true);
				break;
			  }
			}
		  }
		}
	  }
	}
	return baseConfig;
  }

  public boolean updateMenuConfigFile(String userName,
	  ArrayList<String> menuList) {
	boolean isSuc = false;
	MenuConfig baseMenuConfig = getBaseMenuConfigFile();
	MenuConfig userMenuConfig = new MenuConfig();
	List<Menu> userMenuList = new ArrayList<Menu>();
	for (Menu menu : baseMenuConfig.getMenuList()) {
	  List<ChildMenu> userChildMenuList = new ArrayList<ChildMenu>();
	  for (ChildMenu childMenu : menu.getChildMenuList()) {
		for (String um : menuList) {
		  if (um.equals(String.valueOf(childMenu.getId()))) {
			userChildMenuList.add(childMenu);
			break;
		  }
		}
	  }
	  if (userChildMenuList.size() > 0) {
		Menu userMenu = new Menu();
		userMenu.setId(menu.getId());
		userMenu.setIcon(menu.getIcon());
		userMenu.setName(menu.getName());
		userMenu.setChildMenuList(userChildMenuList);
		userMenuList.add(userMenu);
	  }
	}
	userMenuConfig.setMenuList(userMenuList);
	String configFile = "WEB-INF/resources/config/menu/" + userName
	    + "_menu_config.xml";
	try {
	  String path = this.getClass().getResource("").getPath()
		  .replaceAll("%20", " ");
	  String fileName = path.substring(0, path.indexOf("WEB-INF")) + configFile;
	  XML.toXMLFile(userMenuConfig, MenuConfig.class, fileName);
	  EhcacheUtil ehcache = EhcacheUtil.getInstance();
	  Object o = ehcache.get("configFileCache", configFile);
	  if (o != null) {
		ehcache.remove("configFileCache", configFile);
	  }
	  isSuc = true;
	} catch (Exception e) {
	  e.printStackTrace();
	}
	/*
	 * MenuConfig tmp = new MenuConfig(); List<Menu> menuList = new
	 * ArrayList<Menu>(); Menu m = new Menu(); m.setId(1); m.setName("aa");
	 * m.setIcon("aaa"); List<ChildMenu> childMenuList = new
	 * ArrayList<ChildMenu>(); ChildMenu cm = new ChildMenu(); cm.setId(11);
	 * cm.setName("bb"); cm.setParentId(1); cm.setUrl("bbb");
	 * childMenuList.add(cm);
	 * 
	 * cm = new ChildMenu(); cm.setId(12); cm.setName("cc"); cm.setParentId(1);
	 * cm.setUrl("ccc"); childMenuList.add(cm);
	 * 
	 * m.setChildMenuList(childMenuList); menuList.add(m);
	 * tmp.setMenuList(menuList);
	 * 
	 * String aa = XML.toXML(tmp, MenuConfig.class);
	 */
	return isSuc;
  }
}
